package com.gongpingjialoan.widget;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.PopupWindow;

import com.gongpingjialoan.R;
import com.gongpingjialoan.adapter.car.SortAdapter;
import com.gongpingjialoan.bean.car.SortBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/5/17.
 */
public class SortPopopWindow extends PopupWindow implements AdapterView.OnItemClickListener {

    private Context mContext;

    private View mMenuView;

    private ListView listView;

    private List<SortBean> sortBeans;

    private SortAdapter sortAdapter;

    private ISelcted mISelcted;

    public SortBean sortBean;

    private boolean isCutPrice = false;

    //is_certify  = “1”  cs_clean = "true"
    private boolean isGoodCar = false;


    public void setISelcted(ISelcted ISelcted) {
        mISelcted = ISelcted;
    }

    public SortPopopWindow(Context context) {
        super(context);
        this.mContext = context;
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        mMenuView = inflater.inflate(R.layout.sort_pop1, null);

        listView = (ListView) mMenuView.findViewById(R.id.listview);
        mMenuView.findViewById(R.id.price_popup).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        // mMaterialRangeSlider.setMinMax(min ,max);
        //设置SelectPicPopupWindow的View
        this.setContentView(mMenuView);
        //设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        //设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        //设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        this.setOutsideTouchable(true);
        //设置SelectPicPopupWindow弹出窗体动画效果
        //this.setAnimationStyle(R.style.AnimBottom);
        listView.setOnItemClickListener(this);
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable cd = new ColorDrawable(0x000000);
        this.setBackgroundDrawable(cd);
        initData();
        sortAdapter = new SortAdapter(sortBeans, mContext);
        listView.setAdapter(sortAdapter);
        sortAdapter.setPosition(-1);
    }


    private void initData() {
        SortBean sortBean;
        sortBeans = new ArrayList<SortBean>();
        sortBean = new SortBean("默认排序", "");
        sortBeans.add(sortBean);
        sortBean = new SortBean("认证好车", "");
        sortBeans.add(sortBean);
        sortBean = new SortBean("降价好车", "1");
        sortBeans.add(sortBean);
        sortBean = new SortBean("离我最近", "distance");
        sortBeans.add(sortBean);
        sortBean = new SortBean("价格升序", "price");
        sortBeans.add(sortBean);
        sortBean = new SortBean("价格降序", "-price");
        sortBeans.add(sortBean);
        sortBean = new SortBean("最新上牌", "-age");
        sortBeans.add(sortBean);
        sortBean = new SortBean("最新发布", "-pub_time");
        sortBeans.add(sortBean);
        sortBean = new SortBean("里程最短", "mile");
        sortBeans.add(sortBean);
    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (mISelcted != null) {
            if (position == 2) {
                isCutPrice = !isCutPrice;
                sortAdapter.setIsCutPrice(isCutPrice);
                //   sortAdapter.setPosition(position);
                mISelcted.select(sortBean, isCutPrice, isGoodCar);
            } else if (position == 1) {
                isGoodCar = !isGoodCar;
                sortAdapter.setIsGoodcar(isGoodCar);
                mISelcted.select(sortBean, isCutPrice, isGoodCar);
            } else if (position == 0) {
                isCutPrice = false;
                isGoodCar = false;
                sortAdapter.setIsCutPrice(isCutPrice);
                sortAdapter.setIsGoodcar(isGoodCar);
                sortAdapter.setPosition(0);
                sortBean = sortBeans.get(position);
                mISelcted.select(sortBean, isCutPrice, isGoodCar);
            } else {
                sortAdapter.setPosition(position);
                sortBean = sortBeans.get(position);
                mISelcted.select(sortBean, isCutPrice, isGoodCar);
            }
            dismiss();
        }
    }

    public void setGoodCar(boolean goodcar) {
        isGoodCar = goodcar;
        if (sortAdapter != null)
            sortAdapter.setIsGoodcar(isGoodCar);
    }



    public void setCut(boolean cut) {
        isCutPrice = cut;
        if (sortAdapter != null)
            sortAdapter.setIsCutPrice(isCutPrice);
    }


    public void setPosition(int position) {
        if (sortAdapter != null)
            sortAdapter.setPosition(position);
    }


    public interface ISelcted {
        public void select(SortBean bean, boolean isCutPrice, boolean isGoodCar);
    }
}
